-- 排行数据转换
local RankDataConvertHelper = {}
local GuildBaseInfo = require("app.public.dao.GuildBaseInfo")
local UserBaseInfo = require("app.public.dao.UserBaseInfo")



-- 头部 type 1 3列  2 4列
local CSGuildWarTopType = 2
local CSEliteTopType = 1
local CSDuelTopType = 1
-- type = 1  3列   type = 2 4列
-- 底部 类型
function RankDataConvertHelper.getTopDataByType(tp)
    if not RankDataConvertHelper.TopTypeData then
        RankDataConvertHelper.TopTypeData = {
            {type = 1, title1 = L("bossRank_player"), title2 = L("rankcondition_2"), title3 = L("rankcondition_20001")}, --玩家  战力 赛季积分
            {type = 1, title1 = L("bossRank_guildName"), title2 = L("rankcondition_1202"), title3 = L("rankcondition_1203")}, -- 公会名称  公会等级  公会总战力
        }
    end
    return RankDataConvertHelper.TopTypeData[tp]
end


--
RankDataConvertHelper.ContentPlayer = 1
RankDataConvertHelper.ContentPlayer2 = 2
RankDataConvertHelper.ContentGuild = 3
--
RankDataConvertHelper.PageCellNum = 20 -- 每一个页签 最懂可以放下多少页
--
function RankDataConvertHelper.getPageNumByNum(num)
    return math.ceil( num / RankDataConvertHelper.PageCellNum)
end


-- 转换底部 信息
function RankDataConvertHelper.convertToBottomInfo(myRankStr, pageNum, otherInfo)
    if myRankStr == nil then
        myRankStr = L("lua_code_text_153") .. L("lua_code_text_306")
    end
    return {
        myRankStr = myRankStr,
        pageNum = pageNum or 0,
        otherInfo = otherInfo,
    }
end
--

-- 我的数据:x(多少名)
function RankDataConvertHelper.convertToBottomStr1(value, rank)
    if not value then
        value = 0
    end
    if rank then
        return string.format(L("lua_code_text_194"), value, string.format(L("lua_code_text_173"), rank))
    end
end

-- 转换 
-- function RankDataConvertHelper.ConvertToPower()
-- end

-- function RankDataConvertHelper.ConvertToPower()
-- end
RankDataConvertHelper.DataType = {
    CSGuildWar = 1, -- 跨服工会战
    CSArena = 2, --跨服精英赛
    CSDuel = 3,
}
--
function RankDataConvertHelper.getServerNameByServerIndex(index)
    if not index then
        return ""
    end
    return app.channelAPI:getServerNameByIndex(index)
end

function RankDataConvertHelper.convertFightPower(power)
    if not power then
        return 0
    end
    local unitStr =  L("damageunit1")
    if power > 10000 then
        return math.floor(power/10000)..unitStr
    end
    return tostring(power)
end

-- 转换 跨服公会战 排行榜数据
-- pb WorldRankNode
local function convertToCSGuildWar(pbDatas, params)
    local contentData = {}
    local startRank = params.startRank

    for k , v in pairs(pbDatas or {}) do
        if k >= startRank then
            local guildInfo = GuildBaseInfo.new()
            guildInfo:read(rawget(v, "guild"))
            table.insert(contentData, {
                rank = k, 
                tag = RankDataConvertHelper.ContentGuild, 
                guildInfo = guildInfo,
                value1 = guildInfo:getGuildName(),
                value2 = guildInfo:getGuildLv(),
                value3 = guildInfo:getTotalPowerStr(),
                value11 = guildInfo:getServerName(),
            })
        end
    end
    return {
        topData = RankDataConvertHelper.getTopDataByType(CSGuildWarTopType),
        contentData = contentData,
        startRank = params.startRank,
        startPage = params.startPage,
    }
end

-- 跨服 精英赛
-- pb WorldRankNode
local function convertToCSArena(pbDatas, params)
    local contentData = {}
    local startRank = params.startRank

    local getServerNameByServerIndex = RankDataConvertHelper.getServerNameByServerIndex
    local convertFightPower = RankDataConvertHelper.convertFightPower
    for k , v in pairs(pbDatas or {}) do
        if k >=  startRank then
            local userBaseInfo = UserBaseInfo.new()
            userBaseInfo:read(rawget(v, "user"))
            table.insert(contentData, {
                rank = k, 
                tag = RankDataConvertHelper.ContentPlayer, 
                userBaseInfo = userBaseInfo,
                value1 = userBaseInfo.name,
                value11 = getServerNameByServerIndex(userBaseInfo.serverID),
                value2 = convertFightPower(userBaseInfo.combatPower),
                value3 = rawget(v, "value") or 0,
                isShowBtn = false,
            })
        end
    end
    return {
        topData = RankDataConvertHelper.getTopDataByType(CSEliteTopType),
        contentData = contentData,
        startRank = params.startRank,
        startPage = params.startPage,
    }
end

-- 跨服 决斗赛
local function convertToCSDuel(pbDatas, params)
    local contentData = {}
    local startRank = params.startRank

    local getServerNameByServerIndex = RankDataConvertHelper.getServerNameByServerIndex
    local convertFightPower = RankDataConvertHelper.convertFightPower
    for k , v in pairs(pbDatas or {}) do
        if k >=  startRank then
            local userBaseInfo = UserBaseInfo.new()
            userBaseInfo:read(rawget(v, "user"))
            table.insert(contentData, {
                rank = k, 
                tag = RankDataConvertHelper.ContentPlayer, 
                userBaseInfo = userBaseInfo,
                value1 = userBaseInfo.name,
                value11 = getServerNameByServerIndex(userBaseInfo.serverID),
                value2 = convertFightPower(userBaseInfo.combatPower),
                value3 = rawget(v, "value") or 0,
                isShowBtn = false,
            })
        end
    end
    return {
        topData = RankDataConvertHelper.getTopDataByType(CSEliteTopType),
        contentData = contentData,
        startRank = params.startRank,
        startPage = params.startPage,
    }
end


function RankDataConvertHelper.ConvertToData(typeIndex, data, params)
    local DataType = RankDataConvertHelper.DataType
    if typeIndex  == DataType.CSGuildWar then
        -- 跨服公会排行榜
        return convertToCSGuildWar(data, params)
    elseif typeIndex  == DataType.CSArena then
        return convertToCSArena(data, params)
    elseif typeIndex  == DataType.CSDuel then
        return convertToCSDuel(data, params)
    end
end 



return RankDataConvertHelper